﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents;
using WebComponents.Security;
using System.Collections.Generic;
using GBIT.Web;

public partial class NguoiDungList : UserControlBase {
  private List<UserInfo> userList = null;
  protected void Page_Load(object sender, EventArgs e) {
    if ((Session["login"] as UserInfo).IsApproved == false) {
      Response.Redirect("default.aspx");
    }
    this.AddModule = "NguoiDung/ThemNguoiDung.ascx";
    if (!Page.IsPostBack) {
      userList = UserController.ListUser();
      grvUser.DataSource = userList;
      grvUser.DataBind();     
    }
    this.DeleteEnabled = true;
    this.AddEnabled = true;
    this.EditEnabled = true;   
    this.Title = "Danh sách người dùng";
  } 
  protected override void OnDeleteRecord() {
    foreach (GridViewRow row in grvUser.Rows) {
      if (((HtmlInputCheckBox)row.FindControl("chkSelect")).Checked) {
        if (((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].ToString() != ((UserInfo)Session["login"]).UserId.ToString())
          UserController.Delete(new Guid(((HtmlInputCheckBox)row.FindControl("chkSelect")).Attributes["title"].ToString()));
      }
    }
    grvUser.DataSource = UserController.ListUser();
    grvUser.DataBind();
  }
  
  protected void grvUser_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    grvUser.PageIndex = e.NewPageIndex;
    grvUser.DataSource = userList;
    grvUser.DataBind();
  }
  protected void grvUser_Sorting(object sender, GridViewSortEventArgs e) {
    userList = UserController.ListUser();
    userList.Sort(new ObjectComparer<UserInfo>(e.SortExpression, ObjectComparer<UserInfo>.AutoSortDirection));
    grvUser.DataSource = userList;
    grvUser.DataBind();
  }
  protected void grvUser_RowDataBound(object sender, GridViewRowEventArgs e) {

  }
}
